using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Server.TableEFCore;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    internal class ServerRepository
    {
        public ServerRepository(DbContext dbContext)
        {
            _dbContext = dbContext;
        }
        private readonly DbContext _dbContext;

        private AccountsEFCore _accountsEFCore;
        private ActionLogsEFCore _actionLogsEFCore;
        private AdministratorsEFCore _administratorsEFCore;
        private BusinesssEFCore _businesssEFCore;
        private DictionaryTypesEFCore _dictionaryTypesEFCore;
        private DictionarysEFCore _dictionarysEFCore;
        private ErrorLogsEFCore _errorLogsEFCore;
        private GoodsEFCore _goodsEFCore;
        private InfosEFCore _infosEFCore;
        private LoginLogsEFCore _loginLogsEFCore;
        private OrderDetailsEFCore _orderDetailsEFCore;
        private OrdersEFCore _ordersEFCore;
        private PromotionsRepositoryTable _promotionsEFCore;
        private SuppliersEFCore _suppliersEFCore;
        private VarifysEFCore _varifysEFCore;
        private ChartsEFCore _chartsEFCore;
        private CollectionsEFCore _collectionsEFCore;
        private HistorysEFCore _historysEFCore;
        private CommentsEFCore _commentsEFCore;
        private CommentResourcesEFCore _commentResourcesEFCore;
        private SearchRecordsEFCore _searchRecordsEFCore;

        public SearchRecordsEFCore searchRecordsEFCore
        {
            get
            {
                if (_searchRecordsEFCore == null)
                {
                    _searchRecordsEFCore = new SearchRecordsEFCore(_dbContext);
                }
                return _searchRecordsEFCore;
            }
        }

        public CommentResourcesEFCore commentResourcesEFCore
        {
            get
            {
                if (_commentResourcesEFCore == null)
                {
                    _commentResourcesEFCore = new CommentResourcesEFCore(_dbContext);
                }
                return _commentResourcesEFCore;
            }
        }

        public CommentsEFCore commentsEFCore
        {
            get
            {
                if (_commentsEFCore == null)
                {
                    _commentsEFCore = new CommentsEFCore(_dbContext);
                }
                return _commentsEFCore;
            }
        }

        public HistorysEFCore historysEFCore
        {
            get
            {
                if (_historysEFCore == null)
                {
                    _historysEFCore = new HistorysEFCore(_dbContext);
                }
                return _historysEFCore;
            }
        }

        public CollectionsEFCore collectionsEFCore
        {
            get
            {
                if (_collectionsEFCore == null)
                {
                    _collectionsEFCore = new CollectionsEFCore(_dbContext);
                }
                return _collectionsEFCore;
            }
        }

        public ChartsEFCore chartsEFCore
        {
            get
            {
                if (_chartsEFCore == null)
                {
                    _chartsEFCore = new ChartsEFCore(_dbContext);
                }
                return _chartsEFCore;
            }
        }

        public AccountsEFCore accountsEFCore
        {
            get
            {
                if (_accountsEFCore == null)
                {
                    _accountsEFCore = new AccountsEFCore(_dbContext);
                }
                return _accountsEFCore;
            }
        }
        public ActionLogsEFCore actionLogsEFCore
        {
            get
            {
                if (_actionLogsEFCore == null)
                {
                    _actionLogsEFCore = new ActionLogsEFCore(_dbContext);
                }
                return _actionLogsEFCore;
            }
        }
        public AdministratorsEFCore administratorsEFCore
        {
            get
            {
                if (_administratorsEFCore == null)
                {
                    _administratorsEFCore = new AdministratorsEFCore(_dbContext);
                }
                return _administratorsEFCore;
            }
        }
        public BusinesssEFCore businesssEFCore
        {
            get
            {
                if (_businesssEFCore == null)
                {
                    _businesssEFCore = new BusinesssEFCore(_dbContext);
                }
                return _businesssEFCore;
            }
        }
        public DictionaryTypesEFCore dictionaryTypesEFCore
        {
            get
            {
                if (_dictionaryTypesEFCore == null)
                {
                    _dictionaryTypesEFCore = new DictionaryTypesEFCore(_dbContext);
                }
                return _dictionaryTypesEFCore;
            }
        }
        public DictionarysEFCore dictionarysEFCore
        {
            get
            {
                if (_dictionarysEFCore == null)
                {
                    _dictionarysEFCore = new DictionarysEFCore(_dbContext);
                }
                return _dictionarysEFCore;
            }
        }
        public ErrorLogsEFCore errorLogsEFCore
        {
            get
            {
                if (_errorLogsEFCore == null)
                {
                    _errorLogsEFCore = new ErrorLogsEFCore(_dbContext);
                }
                return _errorLogsEFCore;
            }
        }
        public GoodsEFCore goodsEFCore
        {
            get
            {
                if (_goodsEFCore == null)
                {
                    _goodsEFCore = new GoodsEFCore(_dbContext);
                }
                return _goodsEFCore;
            }
        }
        public InfosEFCore infosEFCore
        {
            get
            {
                if (_infosEFCore == null)
                {
                    _infosEFCore = new InfosEFCore(_dbContext);
                }
                return _infosEFCore;
            }
        }
        public LoginLogsEFCore loginLogsEFCore
        {
            get
            {
                if (_loginLogsEFCore == null)
                {
                    _loginLogsEFCore = new LoginLogsEFCore(_dbContext);
                }
                return _loginLogsEFCore;
            }
        }
        public OrderDetailsEFCore orderDetailsEFCore
        {
            get
            {
                if (_orderDetailsEFCore == null)
                {
                    _orderDetailsEFCore = new OrderDetailsEFCore(_dbContext);
                }
                return _orderDetailsEFCore;
            }
        }
        public OrdersEFCore ordersEFCore
        {
            get
            {
                if (_ordersEFCore == null)
                {
                    _ordersEFCore = new OrdersEFCore(_dbContext);
                }
                return _ordersEFCore;
            }
        }
        public PromotionsRepositoryTable promotionsEFCore
        {
            get
            {
                if (_promotionsEFCore == null)
                {
                    _promotionsEFCore = new PromotionsRepositoryTable(_dbContext);
                }
                return _promotionsEFCore;
            }
        }
        public SuppliersEFCore suppliersEFCore
        {
            get
            {
                if (_suppliersEFCore == null)
                {
                    _suppliersEFCore = new SuppliersEFCore(_dbContext);
                }
                return _suppliersEFCore;
            }
        }
        public VarifysEFCore varifysEFCore
        {
            get
            {
                if (_varifysEFCore == null)
                {
                    _varifysEFCore = new VarifysEFCore(_dbContext);
                }
                return _varifysEFCore;
            }
        }
        /// <summary>
        /// 在传入方法中执行的所有操作都会被视为一个事务,错误仍然会抛出但事务会回滚。
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task ExecuteInTransactionAsync(Func<Task> action)
        {
            using var transaction = await _dbContext.Database.BeginTransactionAsync();
            try
            {
                await action();
                await transaction.CommitAsync();
            }
            catch (Exception)
            {
                await transaction.RollbackAsync();
                throw; // 重新抛出异常
            }
        }
        /// <summary>
        /// 在传入方法中执行的所有操作都会被视为一个事务,错误仍然会抛出但事务会回滚。如果外部变量在中受到了更改可以在fail中恢复，如果只是一次请求有效则不必，因为后续代码受报错影响不会执行。
        /// </summary>
        /// <param name="action"></param>
        /// <param name="fail"></param>
        /// <returns></returns>
        public async Task ExecuteInTransactionAsync(Func<Task> action, Func<Task> fail)
        {
            using var transaction = await _dbContext.Database.BeginTransactionAsync();
            try
            {
                await action();
                await transaction.CommitAsync();
            }
            catch (Exception)
            {
                await transaction.RollbackAsync();
                throw; // 重新抛出异常
            }
        }
    }
}